package coml.jiyun.hp.day14_17and20;

import android.os.Bundle;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import coml.jiyun.hp.day14_17and20.adapter.MainAdapter;
import coml.jiyun.hp.day14_17and20.fragment.DbjFragment;
import coml.jiyun.hp.day14_17and20.fragment.HomeFragment;
import coml.jiyun.hp.day14_17and20.fragment.SjkFragment;
import coml.jiyun.hp.day14_17and20.fragment.TheSecondaryListFragment;
import coml.jiyun.hp.day14_17and20.fragment.XmFragment;

public class MainActivity extends AppCompatActivity {

    /**
     * 首页
     */
    private TextView mMainToolTv;
    private Toolbar mMainTool;
    private ViewPager mMainVp;
    private TabLayout mMainTab;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        mMainToolTv = (TextView) findViewById(R.id.main_tool_tv);
        mMainTool = (Toolbar) findViewById(R.id.main_tool);
        mMainVp = (ViewPager) findViewById(R.id.main_vp);
        mMainTab = (TabLayout) findViewById(R.id.main_tab);

        mMainTool.setTitle("");
        setSupportActionBar(mMainTool);

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new DbjFragment());
        fragments.add(new TheSecondaryListFragment());
        fragments.add(new XmFragment());
        fragments.add(new SjkFragment());

        list = new ArrayList<>();
        list.add("首页");
        list.add("多布局");
        list.add("二级列表");
        list.add("项目");
        list.add("数据库");

        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragments,list);
        mMainVp.setAdapter(mainAdapter);
        mMainTab.setupWithViewPager(mMainVp);

        mMainTab.addTab(mMainTab.newTab().setIcon(R.drawable.img_tup));
        mMainTab.addTab(mMainTab.newTab().setIcon(R.drawable.img_tup));
        mMainTab.addTab(mMainTab.newTab().setIcon(R.drawable.img_tup));
        mMainTab.addTab(mMainTab.newTab().setIcon(R.drawable.img_tup));
        mMainTab.addTab(mMainTab.newTab().setIcon(R.drawable.img_tup));


//        mMainTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                int position = tab.getPosition();
//                if (position == 0){
//                    mMainToolTv.setText("首页");
//                }else if (position == 1){
//                    mMainToolTv.setText("多布局");
//                }else if (position == 3){
//                    mMainToolTv.setText("二级列表");
//                }else if (position == 4){
//                    mMainToolTv.setText("项目");
//                }else {
//                    mMainToolTv.setText("数据库");
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
    }
}
